﻿using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.BloodGlucoseMeterNICU
{
    public partial class BloodGlucoseMeterPrintsNICU : Form
    {
        #region 变量声明
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        Tb_User LoginUser;
        DateTime StatDate;
        DateTime EndDate;
        #endregion

        #region 构造函数
        public BloodGlucoseMeterPrintsNICU(Tb_User loginUser, PatientInfo patientInfo, DateTime StatDate, DateTime EndDate)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            this.StatDate = StatDate;
            this.EndDate = EndDate;
            //载入报表模板数据
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\血糖检测表(NICU).grf");
            //设置与数据源的连接串，因为在设计时指定的数据库路径是绝对路径。
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            //连接报表取数事件
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            //设定查询显示器关联的报表
            axGRPrintViewer1.Report = Report;
        }
        #endregion

        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordByDate(this.PatientInfo.PATIENT_ID, this.PatientInfo.VISIT_ID, (int)FormType.BloodGlucoseMeterNICU, StatDate, EndDate);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderBy(p => p.AssessmentDate).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record1 = list[i];
                    BloodMeter boo = XmlUtil.XMLToObject<BloodMeter>(record1.NursingFormData, Encoding.UTF8);
                    DataRow row = dt.NewRow();
                    row["PATIENT_NAME"] = PatientInfo.PATIENT_NAME;
                    row["SEX"] = PatientInfo.SEX;
                    row["AGE"] = PatientInfo.AGE;
                    row["BED_NO"] = PatientInfo.BED_NO;
                    row["PATIENT_ID"] = PatientInfo.PATIENT_ID;
                    row["jianceriqi"] = boo.JianCeRiQi;
                    row["shijian"] = boo.ShiJian;
                    row["xuetangzhi"] = boo.XueTangZhi;
                    row["qianming"] = boo.QianMing;
                    dt.Rows.Add(row);
                }
                dt.DefaultView.Sort = "JianCeRiQi ASC,ShiJian ASC";
                DataTable data = dt.DefaultView.ToTable();
                Utility.FillRecordToReport(Report, data);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        private void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("PATIENT_NAME", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("AGE", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PATIENT_ID", typeof(string));
            dt.Columns.Add("jianceriqi", typeof(string));
            dt.Columns.Add("shijian", typeof(string));
            dt.Columns.Add("xuetangzhi", typeof(string));
            dt.Columns.Add("qianming", typeof(string));
        }

        #region 窗体加载启动锐浪报表
        private void BloodGlucoseMeterPrints_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
        #endregion
    }
}